package com.yk.framework.gui.wf.ui;

import java.awt.Component;
import java.awt.Point;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.event.MouseInputListener;

import com.yk.framework.gui.wf.Contants;

/**
 * @author lifetime
 * 
 */
public class DrawAction implements MouseInputListener {
	WFPanel panel;
	Point p1;
	Point pMove;
	Point p2;
	boolean isPress;
	boolean isMove;

	public DrawAction(WFPanel panel) {
		this.panel = panel;
	}

	public void mouseClicked(MouseEvent e) {
		pMove = null;
		if (!panel.isDoingDrawLine && !panel.isDoingDrawNode) {
			Component[] coms = panel.getComponents();
			for (int i = 0; i < coms.length; i++) {
				if (coms[i] != null && coms[i] instanceof JLabel) {
					JLabel l = (JLabel) coms[i];
					l.putClientProperty("isSelected", Boolean.FALSE);
					if (l.getClientProperty(Contants.IsLineLabel) == Boolean.TRUE) {
						l.setBorder(BorderFactory.createEmptyBorder());
					}
					l.invalidate();
				}
			}
			panel.repaint();
		}
		panel.requestFocusInWindow();
	}

	public void mouseEntered(MouseEvent e) {

	}

	public void mouseExited(MouseEvent e) {

	}

	public void mousePressed(MouseEvent e) {
		if (panel.isDoingDrawLine || panel.isDoingDrawNode) {
			if (SwingUtilities.isLeftMouseButton(e)) {
				p1 = e.getPoint();
			}
		} else {
			p1 = null;
		}
		isPress = true;
		isMove = false;
		panel.requestFocusInWindow();
	}

	public void mouseReleased(MouseEvent e) {
		if (panel.isDoingDrawNode) {
			panel.drawImageLabel(panel.getCurrentNode().getName(), panel.getCurrentNode().getNodeType(), panel.getCurrentNode().getIconName(), p1, null);
			panel.setDoingDrawNode(false);
			panel.resetState();
		}
		if (isPress && isMove) {
			if (panel.isDoingDrawLine) {
				p2 = e.getPoint();
				panel.drawLine(p1, p2);
			} else {
				p2 = null;
			}
		} else {
			p2 = null;
		}
		isPress = false;
		isMove = false;
		panel.requestFocusInWindow();
	}

	public void mouseDragged(MouseEvent e) {
		isMove = true;
		if (panel.isDoingDrawLine) {
			pMove = e.getPoint();
		} else {
			pMove = null;
		}
		panel.repaint();
	}

	public void mouseMoved(MouseEvent e) {

	}

	public Point getP1() {
		return p1;
	}

	public void setP1(Point p1) {
		this.p1 = p1;
	}

	public Point getpMove() {
		return pMove;
	}

	public void setpMove(Point pMove) {
		this.pMove = pMove;
	}

	public Point getP2() {
		return p2;
	}

	public void setP2(Point p2) {
		this.p2 = p2;
	}

}
